package com.aco.party.service.system;

import com.aco.party.dao.custom.QuestionDao;
import com.aco.party.enums.QuestionStatusEnum;
import com.aco.party.enums.VisitTypeEnum;
import com.aco.party.model.po.Question;
import com.aco.party.model.po.User;
import com.aco.party.model.po.Visit;
import com.aco.party.model.vo.QuestionParam;
import com.aco.party.model.vo.QuestionVo;
import com.aco.party.model.vo.VisitParam;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class QuestionService {
    @Resource
    private QuestionDao questionDao;

    @Resource
    private UserService userService;

    @Resource
    private VisitService visitService;

    public List<QuestionVo> getByPage(PageParam pageParam,QuestionParam param) {
        List<QuestionVo> list = new ArrayList<>();
        List<Question> questions = questionDao.getList(pageParam,param);
        QuestionVo vo;
        if (questions != null) {
            for (Question question : questions) {
                vo = new QuestionVo();
                BeanUtils.copyProperties(question, vo);
                User user = userService.getUser(vo.getUserId());
                if(user != null) {
                    vo.setCreatorStr(user.getNickName());
                    vo.setUserPhoto(user.getPhotoUrl());
                }
                if(QuestionStatusEnum.推荐.getDesc().equals(question.getStatus())){
                    vo.setIsUp(1);
                }

                VisitParam visitParam = new VisitParam();
                visitParam.setTargetId(question.getId());
                visitParam.setType(VisitTypeEnum.问答点赞.getValue());
                vo.setLikes(visitService.getCountByParam(visitParam));
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(QuestionParam param) {
        return questionDao.getTotal(param);
    }

    public void update(Question question) {
        question.setUpdateTime(new Date());
        questionDao.updateByPrimaryKeySelective(question);
    }

    public void add(Question question) {
        question.setCreateTime(new Date());
        questionDao.insertSelective(question);
    }

    public void delete(Long id) {
        questionDao.deleteByPrimaryKey(id);
    }

    public Question getByPrimaryKey(Long id) {
        return questionDao.selectByPrimaryKey(id);
    }

    public QuestionVo getVoById(Long id) {
        Question question = questionDao.selectByPrimaryKey(id);
        QuestionVo vo = new QuestionVo();
        BeanUtils.copyProperties(question, vo);
        User user = userService.getUser(vo.getUserId());
        if(user != null) {
            vo.setCreatorStr(user.getNickName());
            vo.setUserPhoto(user.getPhotoUrl());
        }
        VisitParam visitParam = new VisitParam();
        visitParam.setTargetId(question.getId());
        visitParam.setType(VisitTypeEnum.问答点赞.getValue());
        vo.setLikes(visitService.getCountByParam(visitParam));
        return vo;
    }
}
